package cn.hongsw.monitor.task;

import org.apache.commons.io.filefilter.IOFileFilter;

import java.io.File;
import java.util.List;

/**
 * 自定义文件过滤器
 *
 * @author :hongsw
 * @date :2021-10-11 11:16
 */
public class FileFilter implements IOFileFilter {

    /**
     * 需要监控的文件类型
     */
    private final List<String> needMonitorFile;

    public FileFilter(List<String> needMonitorFile) {
        this.needMonitorFile = needMonitorFile;
    }


    /**
     * 判断文件是否需要被监控
     *
     * @param file {@link java.io.File} 监控目录下的文件
     * @return {@link boolean}
     * @author hongsw
     * @date 2021/10/13 16:28
     **/
    @Override
    public boolean accept(File file) {
        String[] split = file.getName().split("\\.");
        String fileType = split[split.length - 1];
        return needMonitorFile.stream().anyMatch(i -> i.equalsIgnoreCase(fileType)) || file.isDirectory();
    }

    @Override
    public boolean accept(File file, String s) {
        System.out.println(s);
        return false;
    }
}
